﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.SellOrder;
using System.Collections;
using UI.Lib.Factory;
using UI.Lib;

namespace UI.StorageManagement
{
    public partial class FrmOrdSend : UI.ParentForm.BaseMain
    {
        FrmOrdSendMedit edit = null;
        CheckBox chkPrePrint = null; //仅未打印配货单
        bool IsRePrintRight = false;
        ToolStripItem _tspSendComplate = null;

        public FrmOrdSend(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();

            Hashtable hsWhere = new Hashtable();
            hsWhere["modulecode"] = this.ModuleCode;
            hsWhere["actioncode"] = "R";
            hsWhere["usercode"] = Context.User.Uesrcode;

            //查询数据库是否拥有重新打印的权限
            DataTable dt = CommonHelper.Entity.ExecFun("checkRight", hsWhere);

            IsRePrintRight = (null != dt && 0 != dt.Rows.Count);
        }

        protected override void AddToolButton()
        {
            int firstBtnIndex = this.tspTop.Items.IndexOf(tspAdd);
            //将打印配货但按钮加入在发货按钮之前
            this.tspTop.Items.Insert(firstBtnIndex, base.CreateToolBtn("P", "打印配货单", new EventHandler(OnPrintBtnClick)));

            tspAdd.Text = "发货";

            this.tspTop.Items.Add(base.CreateToolBtn("O", "打印发货单", new EventHandler(OnPrintSendDocnoBtnClick)));

            _tspSendComplate = base.CreateToolBtn("W", "发货完成", tspSendComplate_Click);
            this.tspTop.Items.Add(_tspSendComplate);

            //添加方便的复选框
            this.tspTop.Items.Add(base.CreateToolCheckBox("CX", "仅未打印配货单", true, "",
                    DoBinder, out chkPrePrint));
        }

        void tspSendComplate_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;

            edit = new FrmOrdSendMedit(this.ModuleCode, "W", GetTitleStyle(this.Text, _tspSendComplate.Text),
                Components.Validation.ToString(row.Cells["listid"].Value));

            edit.ShowDialog();
            base.BindGrid();
        }

        void DoBinder(object sender, EventArgs e)
        {
            BindGrid();
        }

        //点击回单输入后执行
        void OnBackBtnClick(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;

            edit = new FrmOrdSendMedit(this.ModuleCode, "B", GetTitleStyle(this.Text, "回单录入"),
                Components.Validation.ToString(row.Cells["listid"].Value));

            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        void OnPrintSendDocnoBtnClick(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;

            var frm = new FrmSendMain(this.ModuleCode, "O", Components.Validation.ToString(row.Cells["listid"].Value));
            frm.ShowDialog();
        }

        //点击打印配货单后执行
        void OnPrintBtnClick(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;

            var srcBtn = sender as ToolStripButton;

            edit = new FrmOrdSendMedit(this.ModuleCode, Components.Validation.ToString(srcBtn.Tag), GetTitleStyle(this.Text, srcBtn.Text),
                Components.Validation.ToString(row.Cells["listid"].Value),
                Components.Validation.ToString(row.Cells["audstatus"].Value));

            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void OnViewClick(ToolStripButton btn, DataGridViewRow row)
        {
            if (null == row) return;

            edit = new FrmOrdSendMedit(this.ModuleCode, "V", GetTitleStyle(this.Text, btn.Text),
                Components.Validation.ToString(row.Cells["listid"].Value));

            edit.ShowDialog();
        }

        protected override void OnAddClick(ToolStripButton btn)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;

            edit = new FrmOrdSendMedit(this.ModuleCode, "A", GetTitleStyle(this.Text, btn.Text),
                Components.Validation.ToString(row.Cells["listid"].Value));

            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void UpdatePager(UI.Common.Pager pager)
        {
            pager.FilterAppName = "a.muser";

            //未删除且审核过的订单才能进行发货处理
            pager.StrWhere = " a.status <> '9' AND a.audstatus IN ('2', '0') ";

            //仅仅查看未打印配货单的数据
            if (null == chkPrePrint || chkPrePrint.Checked == true)
            {
                pager.StrWhere += " AND a.status = '1'";
            }
        }

        /// <summary>
        /// 行改变时触发
        /// </summary>
        /// <param name="rowIndex"></param>
        protected override void AfterRowChanged(int rowIndex)
        {
            if (rowIndex == -1) return;

            string status = Components.Validation.ToString(this.dgvMain["audstatus", rowIndex].Value);

            ToolStripItem[] items = this.tspTop.Items.Find("btn_P", false);
            ToolStripItem tspPrint = (0 == items.Length) ? null : items[0];

            //打印发货单
            items = null;
            items = this.tspTop.Items.Find("btn_O", false);
            ToolStripItem tspPrint2 = (0 == items.Length) ? null : items[0];

            tspAdd.Enabled = false;
            if (tspPrint != null) tspPrint.Enabled = false;
            if (tspPrint2 != null) tspPrint2.Enabled = false;
            _tspSendComplate.Enabled = false;

            if ("1" == status)
            {
                if (tspPrint != null) tspPrint.Enabled = true;
                tspAdd.Enabled = true;
                _tspSendComplate.Enabled = true;
            }
            else if ("11" == status)
            {
                if (tspPrint != null && IsRePrintRight) tspPrint.Enabled = true;
                if (tspPrint2 != null && IsRePrintRight == true) tspPrint2.Enabled = true;
                tspAdd.Enabled = true;
                _tspSendComplate.Enabled = true;
            }
            else if ("2" == status) //已打印出货单
            {
                if (tspPrint2 != null) tspPrint2.Enabled = true;
            }
            else if ("3" == status) //已配货
            {
                if (tspPrint2 != null && IsRePrintRight == true) tspPrint2.Enabled = true;

                _tspSendComplate.Enabled = true;
            }
            else if ("4" == status) //已发货
            {
                if (tspPrint2 != null && IsRePrintRight == true) tspPrint2.Enabled = true;
            }
        }
    }
}
